<html>
<head>
	<title>Excersise 6</title>
	<link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
	<div id="js-console"></div>
	<script src="scripts/js-console.js"></script>
	<script>
		function Person(firstName, lastName, age) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
            this.toString = function () {
                return sprintf("%s %s (age %d)", this.firstName, this.lastName, this.age);
            }
        }
        function group(people, prop) {
            switch (prop) {
                case "firstName":
                case "lastName":
                case "age":
                    {
                        var groups = {};

                        people.map(function (p) {
                            if (!groups[p[prop]])
                                groups[p[prop]] = new Array();
                            groups[p[prop]].push(p);
                        });

                        return groups;
                    }
                default:
                    throw new Error("No such property in Person.");
            }
        }
        (function () {
            var people = [];
            people.push(new Person("Gosho", "Dimitriv", 20));
            people.push(new Person("Petko", "Ivanov", 25));
            people.push(new Person("Pavel", "Nedved", 15));
            people.push(new Person("Ivan", "Ivanov", 18));
            people.push(new Person("Lyubomir", "Gadjev", 31));
            people.push(new Person("Petar", "Petrov", 82));
            people.push(new Person("Daniel", "Ivanov", 35));
            people.push(new Person("Petkov", "Lyubomirov", 42));
            people.push(new Person("Daniel", "Dimitrov", 52));
            people.push(new Person("Vlado", "Lichahin", 30));
            people.push(new Person("Fedor", "Emelianenko", 63));

            var groups = group(people, "firstName");

            for (var entry in groups) {
                jsConsole.writeLine(sprintf("Group '%s': %s", entry, groups[entry]));
            }
        })();
	</script>
</body>
</html>
